﻿// *********************************************************************
// [DCOM Productions]
// [Copyright (C) DCOM Productions All rights reserved.]
// *********************************************************************

namespace BitFlex.Xdf.Collections {
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    /// <summary>
    /// Base collection of <see cref="XdfSection"/> elements
    /// </summary>
    public class XdfSectionList : List<XdfSection> {
        /// <summary>
        /// Initializes a new instance of the BitFlex.Xdf.Collections.XdfSectionList class
        /// </summary>
        public XdfSectionList()
            : base() {
        }

        /// <summary>
        /// Initializes a new instance of the BitFlex.Xdf.Collections.XdfSectionList class
        /// </summary>
        public XdfSectionList(int capacity)
            : base(capacity) {
        }

        /// <summary>
        /// Initializes a new instance of the BitFlex.Xdf.Collections.XdfSectionList class
        /// </summary>
        public XdfSectionList(IEnumerable<XdfSection> collection)
            : base(collection) {
        }

        /// <summary>
        /// Gets or sets an XdfSection in the collection by name
        /// </summary>
        /// <param name="name">The name of the section</param>
        /// <returns>If using the Get accessor, returns the XdfSection whose name matches the name parameter</returns>
        public XdfSection this[string name] {
            get {
                return Find(s => s.Name.ToLower() == name.ToLower());
            }
            set {
                this[this.IndexOf(Find(s => s.Name.ToLower() == name.ToLower()))] = value;
            }
        }

        /// <summary>
        /// Gets or sets the <see cref="XdfSection"/> that owns this collection of child sections
        /// </summary>
        public XdfSection Owner {
            get;
            set;
        }

        /// <summary>
        /// Adds the specified item to the collection
        /// </summary>
        /// <param name="item">The item to add</param>
        new public void Add(XdfSection item) {
            item.Parent = Owner;
            base.Add(item);
        }

        /// <summary>
        /// Adds the specified collection of items to the collection
        /// </summary>
        /// <param name="collection">The collection to add</param>
        new public void AddRange(IEnumerable<XdfSection> collection) {
            foreach (XdfSection item in collection) {
                item.Parent = Owner;
            }
            base.AddRange(collection);
        }
    }
}